package com.mashibing;

import com.mashibing.annotation.AutoWired;
import com.mashibing.controller.UserController;
import com.mashibing.service.UserService;
import org.junit.Test;

import java.util.stream.Stream;


/**
 * @author 张晓
 * @create 2020-12-09 14:16
 */
public class MyTest2 {

    @Test
    public void test() throws Exception {
        //创建对象
        UserController userController = new UserController();
        //获取类对象
        Class<? extends UserController> clazz = userController.getClass();
        UserService userService = new UserService();
        //获取所有的属性值
        Stream.of(clazz.getDeclaredFields()).forEach(field -> {
            String name = field.getName();
            AutoWired annotation = field.getAnnotation(AutoWired.class);
            if (annotation != null) {
                field.setAccessible(true);
                //获取属性的类型
                Class<?> type = field.getType();
                try {
                    Object o = type.newInstance();
                    field.set(userController, o);
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        });
        System.out.println(userController.getUserService());
    }
}
